import datetime
from google.appengine.ext import db
from django.utils import simplejson
from commons import PickleProperty

class KegDroid(db.Model):
	android_id = db.StringProperty(required=False)
	name = db.StringProperty(required=False)
	#Location of KegDroid
	lat = db.FloatProperty()
	lon = db.FloatProperty()
	beer_id_tap_zero = db.IntegerProperty(required=False)
	beer_id_tap_one = db.IntegerProperty(required=False)
	volume_remaining_tap_zero = db.FloatProperty()
	volume_remaining_tap_one = db.FloatProperty()
	number_drinks_poured = db.IntegerProperty(required=False)
	total_volume_poured = db.FloatProperty()
	last_beer = db.IntegerProperty(required=False)
	last_drinker = db.StringProperty(required=False)
	authorized_drinkers = db.StringListProperty()
	number_drinkers_authorized = db.IntegerProperty(required=False)
	unique_beers = db.StringListProperty()
	unique_beer_count = db.IntegerProperty(required=False)

	def to_dict(self):
		SIMPLE_TYPES = (int, long, float, bool, dict, basestring, list)
		output = {}
		output["ID"] = self.key().id()
		for key, prop in self.properties().iteritems():
			value = getattr(self, key)
			if isinstance(value, SIMPLE_TYPES):
				output[key] = value
			elif value is None:
				output[key] = ""
			elif isinstance(value, datetime.date):
				# Convert date/datetime to ms-since-epoch ("new Date()").
				ms = time.mktime(value.utctimetuple())
				ms += getattr(value, 'microseconds', 0) / 1000
				output[key] = int(ms)
			elif isinstance(value, db.Model):
				output[key] = to_dict(value)
			else:
				raise ValueError('cannot encode ' + repr(prop))
		return output
		#return simplejson.dumps(output).replace("\/", "/")